<?PHP
/* $Id: news.php 5 2009-03-24 20:20:36Z ronan $ */
//edited by Slyferz

session_start();
//getting the user's access to see if they are allowed
include("includes/paxsql.php");
mysql_close();

include 'includes/database.php';
include 'includes/library.php';

function listNews() {
	$time30daysago = eveTime() - 60*60*24*30;
	
	$query = "SELECT news.dateTime, news.type, characters.charID, characters.name FROM news, characters
		WHERE news.charID = characters.charID
		AND news.dateTime > {$time30daysago}
		ORDER BY dateTime DESC";
	$results = mysql_query($query);
	
	echo "<table class=\"main\"><tr><td class=\"main\" width=\"0%\"><b>Date</b></td>
		<td class=\"main\" width=\"100%\"><b>News</b></td></tr>";
	while ($news = mysql_fetch_array($results)) {
		$time = date('n/j/y', $news['dateTime']);
		switch ($news['type']) {
			case 1:	echo "<tr><td class=\"main\">{$time}</td><td class=\"main\">{$news['name']} has left the corp";
					/*** CHECKING FOR REMAINING ALTS OR MAINS***/
					$mainQuery = "SELECT name
						FROM characters, alts
						WHERE characters.charID = alts.altOf
						AND alts.charID = '".$news['charID']."'";
					$mainResults = mysql_query($mainQuery) or die(mysql_error());
					$main = mysql_fetch_array($mainResults);
					$mainName = $main['name'];
					$altquery = "SELECT characters.charID, characters.name 
						FROM characters, alts
						WHERE alts.charID = characters.charID
						AND alts.altOf = '{$news['charID']}'
						AND characters.inCorp = 1
						ORDER BY characters.name";
					$altresults = mysql_query($altquery);
					$altnum = mysql_num_rows($altresults);
					if ($altnum == 0)
					{
						if(empty($mainName))
					       		echo ", leaving behind no alts";
						else
							echo ", leaving ".$mainName." as their main";
					}
					elseif ($altnum == 1) echo ", leaving behind 1 alt (";
					elseif ($altnum > 1) echo ", leaving behind {$altnum} alts (";
					for ($i=0; $i < $altnum; $i++) {
						$alt = mysql_fetch_array($altresults);
						if ($i+1 < $altnum) echo "{$alt['name']}, ";
						else echo "{$alt['name']}";
					}
					if ($altnum > 0) echo ")";
					/*** END CHECKING FOR REMAINING ALTS ***/
					echo ".</td></tr>";
					break;
			case 2:	echo "<tr><td class=\"main\">{$time}</td><td class=\"main\">{$news['name']} has joined the corp.</td></tr>";
					break;
			case 3: echo "<tr><td class=\"main\">{$time}</td><td class=\"main\">{$news['name']} has been autoflagged as inactive.</td></tr>";
					break;
			case 4: echo "<tr><td class=\"main\">{$time}</td><td class=\"main\">{$news['name']}'s main has left the corp.</td></tr>";
					break;
			case 5: echo "<tr><td class=\"main\">{$time}</td><td class=\"main\">{$news['name']} has been returned from inactivity.</td></tr>";
					break;
		}
	}
	echo "</table>";
}

if($access > 1)
{
echo("
<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css'>
<title>snowflake</title>");
include('includes/javascript.php');
echo("
</head>

<body>

<div id='wrapper'>

	<div id='header'><a href='/'>snowflake</a></div>

	<div id='navi'>");
include('includes/navi.php');
echo("</div>

	<div id='main'>
		<h1>news</h1>
		<hr>");

listNews();
echo("
	</div>
	
</div>

</body>

</html>");
}
//they do not belong here, they get the boot
else
{
	header("Location: http://".$_SERVER['HTTP_HOST']."/index.php");
}
?>
